layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 会员用户管理
     */
    var Player = {
        tableId: "playerTable"
    };

    /**
     * 初始化表格的列
     */
    Player.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id',  title: 'id'},
            {field: 'phone', sort: true, align:'center',title: '手机号'},
            {field: 'userNo', sort: true, align:'center',title: '会员号'},
            {field: 'nickname', sort: true,align:'center', title: '姓名'},
            {field: 'userhead', sort: true,align:'center', title: '头像',
                templet:'#pic_big'
            },
            {field: 'company', sort: true,align:'center', title: '公司'},
            {field: 'address', sort: true,align:'center', title: '地址'},
            {field: 'token', hide: true,sort: true,align:'center',  title: '钥匙'},
            {field: 'balance', sort: true,align:'center', title: '余额',
                templet:function (d) {
                    return Feng.formatCurrency(d.balance);
                }
            },
            {field: 'status',hide: true, sort: true,align:'center', title: '1 - 有效；0 - 无效；2-冻结'},
            {field: 'deptid',hide: true, sort: true, align:'center',title: '部门id'},
            {field: 'introducerId', hide: true, align:'center',sort: true, title: '推荐人id'},
            {field: 'lastLogin', sort: true,align:'center', title: '最后登录时间'},
            {field: 'loginStatus',hide: true, align:'center',sort: true, title: '登录状态 0：下线 1：在线'},
            {field: 'loginExpireTime',hide: true,align:'center', sort: true, title: '登录过期时间'},
            {field: 'createTime', sort: true, align:'center',title: '新增时间'},
            {field: 'updateTime', hide: true,align:'center',sort: true, title: '修改时间'},
            {field: 'recordStatus',hide: true, align:'center',sort: true, title: '记录状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minwidth:180,}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Player.search = function () {
        var queryData = {};
        queryData['phone'] = $("#phone").val();
        queryData['userNo'] = $("#userNo").val();
        queryData['nickname'] = $("#nickname").val();
        table.reload(Player.tableId, {where: queryData});
    };

    /**
     * 弹出添加对话框
     */
    Player.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加会员用户',
            content: Feng.ctxPath + '/player/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Player.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Player.exportExcel = function () {
        var checkRows = table.checkStatus(Player.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Player.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改会员用户',
            content: Feng.ctxPath + '/player/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Player.tableId);
            }
        });
    };

    Player.openRestDlg = function (data) {
        admin.putTempData('formOk', false);
        layui.admin.open({
            type: 2,
            title: '会员用户余额',
            maxmin:true,
            area:["100%","100%"],
            content: Feng.ctxPath + '/balanceHistory?playerId=' + data,
            end: function () {
                admin.getTempData('formOk') && table.reload(Player.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Player.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/player/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Player.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Player.tableId,
        url: Feng.ctxPath + '/player/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Player.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Player.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Player.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Player.exportExcel();
    });
    $('#rest').click(function () {
        var checkRows = table.checkStatus(Player.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请先选择用户");
        } else {
            console.log(checkRows.data)
            Player.openRestDlg(checkRows.data[0].id);
        }
    });

    // 工具条点击事件
    table.on('tool(' + Player.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Player.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Player.onDeleteItem(data);
        }
    });
});
